package com.five.years.common.utils;

import java.lang.reflect.Field;
import java.math.BigDecimal;

public class BeanUtils extends org.springframework.beans.BeanUtils {

    public static boolean equals(Object src, Object target) {
        if (src == null && target == null) {
            return true;
        } else if (src != null && target != null) {
            return src.equals(target);
        } else {
            return false;
        }
    }

    public static Object setDefaultValue(Object source) throws IllegalAccessException {
        Field[] fields = source.getClass().getDeclaredFields();
        for(Field f: fields){
            f.setAccessible(true);
            if(f.get(source) == null) {
                if ("java.lang.String".equals(f.getType().getName())) {
                    f.set(source, "");
                }else if("java.lang.Long".equals(f.getType().getName())){
                    f.set(source, 0L);
                }else if("java.lang.Integer".equals(f.getType().getName())){
                    f.set(source, 0);
                }else if("java.math.BigDecimal".equals(f.getType().getName())){
                    f.set(source, new BigDecimal(0));
                }
            }
        }
        return source;
    }

}
